package com.milhouz.quizzdroid.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;

import com.milhouz.quizzdroid.R;
import com.milhouz.quizzdroid.bean.Question;

public class ResponseListLayout extends TableLayout implements IResponsesLayout {

	public static String mCorrectResponse;

	private static Button mResponse1;
	private static Button mResponse2;
	private static Button mResponse3;
	private static Button mResponse4;

	public ResponseListLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setStretchAllColumns(true);
		LayoutInflater.from(context).inflate(R.layout.game_responses, this, true);
		mResponse1 = ((Button) findViewById(R.id.response1));
		mResponse2 = ((Button) findViewById(R.id.response2));
		mResponse3 = ((Button) findViewById(R.id.response3));
		mResponse4 = ((Button) findViewById(R.id.response4));

	}

	@Override
	public void setQuestion(Question question) {
		mCorrectResponse = question.getResponse();
		mResponse1.setText(question.getResponses().get(0).getResponse());
		mResponse2.setText(question.getResponses().get(1).getResponse());
		mResponse3.setText(question.getResponses().get(2).getResponse());
		mResponse4.setText(question.getResponses().get(3).getResponse());
	}

	@Override
	public void setOnResponseListener(final OnResponseListener responseListener) {
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.response1:
					setButtonColorResponse(mResponse1);
					responseListener.onSelectResponse(0);
					break;
				case R.id.response2:
					setButtonColorResponse(mResponse2);
					responseListener.onSelectResponse(1);
					break;
				case R.id.response3:
					setButtonColorResponse(mResponse3);
					responseListener.onSelectResponse(2);
					break;
				case R.id.response4:
					setButtonColorResponse(mResponse4);
					responseListener.onSelectResponse(3);
					break;
				}
			}
		};
		mResponse1.setOnClickListener(listener);
		mResponse2.setOnClickListener(listener);
		mResponse3.setOnClickListener(listener);
		mResponse4.setOnClickListener(listener);
		mResponse1.setBackgroundResource(R.drawable.btn_black);
		mResponse2.setBackgroundResource(R.drawable.btn_black);
		mResponse3.setBackgroundResource(R.drawable.btn_black);
		mResponse4.setBackgroundResource(R.drawable.btn_black);
	}

	private void setButtonColorResponse(Button button) {
		if (!mCorrectResponse.equalsIgnoreCase(String.valueOf(button.getText()))) {
			button.setBackgroundResource(R.drawable.btn_red);
		}
	}

	@Override
	public void showCorrectResponse() {
		if (mCorrectResponse.equalsIgnoreCase(String.valueOf(mResponse1.getText()))) {
			mResponse1.setBackgroundResource(R.drawable.btn_green);
		} else if (mCorrectResponse.equalsIgnoreCase(String.valueOf(mResponse2.getText()))) {
			mResponse2.setBackgroundResource(R.drawable.btn_green);
		} else if (mCorrectResponse.equalsIgnoreCase(String.valueOf(mResponse3.getText()))) {
			mResponse3.setBackgroundResource(R.drawable.btn_green);
		} else if (mCorrectResponse.equalsIgnoreCase(String.valueOf(mResponse4.getText()))) {
			mResponse4.setBackgroundResource(R.drawable.btn_green);
		}
	}
}
